package cn.fbs.service;

import cn.fbs.dao.dm.DMCollectionUnitMapper;
import cn.fbs.model.dm.TCollectionUnitinfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: qbcz
 * @description: 查询采集单位树形结构
 * @author: 张丽婧
 * @create: 2021-05-07 18:51
 **/
@Service
public class CollectionUnitTreeService {
  @Autowired
  private DMCollectionUnitMapper dmCollectionUnitMapper;

  public List<TCollectionUnitinfo> getCollectionUnitTree() {

    List<TCollectionUnitinfo> list = new ArrayList<>();
    List<TCollectionUnitinfo> tCollectionUnitinfos = dmCollectionUnitMapper.getCollectionUnitTree();
    for (TCollectionUnitinfo tCollectionUnitinfo : tCollectionUnitinfos) {
      if (tCollectionUnitinfo.getBdxh().length() == 4) {
        list.add(tCollectionUnitinfo);
      }
      for (TCollectionUnitinfo it : tCollectionUnitinfos) {
        String parentXh = it.getBdxh();
        if (parentXh.substring(0, parentXh.length() - 2).equals(tCollectionUnitinfo.getBdxh())) {
          if (tCollectionUnitinfo.getChildren() == null) {
            tCollectionUnitinfo.setChildren(new ArrayList<TCollectionUnitinfo>());
          }
          tCollectionUnitinfo.getChildren().add(it);

        }
      }
    }

    return list;

  }

}
